// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var css_param = require("./createPickupDetail1.css");
	var css_common = require("../../../css/common.css");
	var tools = require("tools");
	var databind = require("databind");
	var constant = require("constant");
	var html_signFileSealList = require("./html_signFileSealList.html");
	var pickupId;
	var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
	var html_bulkstockInfo = require("./html_bulkstockInfo.html");//查询货物弹出框内容
	var init = function(page) {
		pickupId = page.params.pickupId || base.getUrlParam("pickupId");
		initPickUpDetail(pickupId);
		initEvent();
	};
	var initEvent = function() {
		$("#createPickupDetail1").on("click", ".showSignRecord", function(){
			var $this = $(this);
			
			layer.open({
			  type: 1,
			  title: "签章记录",
			  skin: 'layui-layer-rim', //加上边框
			  area: ['820px', '500px'], //宽高
			  content: html_signFileSealList,
			  btn: ['确定']
			});
			
			tools.loadList("inquireSignFileSealInfoPageList.json", $(".signFileSealList table"), $(".signFileSealList script"), {"fileUuid": $this.parents("tr").attr("fileUuid")}, function(){
				
			}, $(".signFileSealList .pagerCon"));
			
			$(".signFileSealList .cancelPos").unbind("click").click(function(){
				var $this1 = $(this);
				//询问框
				ii2 = layer.confirm('是否确认取消该签章', {
				  btn: ['确定','取消'] //按钮
				}, function(){
				  cancelSealPos($this1.parents("tr").attr("signFileSealId"), $this.parents("tr").attr("fileUuid"));
				}, function(){
				  
				});
			});
		});
	};
	
	//提货详情
	var initPickUpDetail = function(pickupId) {
		base.fnAjax({
			action: "inquireFinancePickupApplicationWarehouse.json",
			async: false,
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas.length > 0) {
						
						databind.fillForm($("#createPickupDetail1"), data.datas[0]);
						
						var pickUpInfoTmpl = $("#createPickupDetail1 #pickUpInfoTmpl").html();
						var html = juicer(pickUpInfoTmpl, data);
						$("#createPickupDetail1 .content1").html(html);
						
						if(data.datas[0].filesUuid != undefined) {
							var filesUuid = data.datas[0].filesUuid;
							var tableBody = "";
							var index = 1;
							$.each(filesUuid.split(","), function() {
								var _this = this;
								if($.trim(_this) != "") {
									tableBody += '<tr><td>'+index+'</td><td>'+ tools.inquireFile(_this) +'</td><td><a target="_blank" class="table_details_link" fileUuid="' + _this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + _this + '" >' + "预览" + '</a></td></tr>';
								}
								index = index + 1;
							});
							
							$("#createPickupDetail1 .fileUuidBody").html(tableBody);
							databind.disableInput("#createPickupDetail1");
						}
						
						$("#createPickupDetail1 .pickupForm table").html("<tr><th><input type='checkbox' class='checkAll none'></th><th>No</th><th>文件名称</th><th>文件类型</th><th>状态</th><th class='none'>签署记录</th><!--<th>退回原因</th>--><th>操作</th></tr>")
						var i = 1;
						$.each(data.datas[0].financeFilesInfoList, function() {
							var isSign = this.isSign;
							var filesInfoId = this.filesInfoId;
							var signState = this.fileSignState.split(',');
							var msg = [];
							if (this.msg != null) {
								msg = this.msg.split(',');
							}
							var j = 0;
							var fs1 = this.filesUuid.split(',');
							var ss1 = this.isSign.split(',');
	//						$.each(this.filesUuid.split(','), function() {
							for (var x = 0; x < fs1.length; x++) {
								var _this = fs1[x];
								if ($.trim(_this) != "") {
									var m = msg[j];
									if (m == null || m == "null") {
										m = "";
									}
									var str;
									if (ss1[x] == '1') {
										str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input checked='checked' type='checkbox' class='checks none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>签署文件</td><td>"+constant.getValueByKey(73, signState[j])+"</td><td class='none'><a href='javascript:void(0);' class='showSignRecord'>查看</td><!--<td>"+m+"</td>--><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
									} else {
										str = "<tr signState='"+signState[j]+"' fileUuid='"+_this+"' filesInfoId='"+filesInfoId+"' ><td><input type='checkbox' disabled='disabled' class='none'></td><td>"+i+"</td><td>"+tools.inquireFile(_this)+"</td><td>附件</td><td>-</td><!--<td></td>--><td><a target='_blank' href='/finance/api/v1.0/viewPdfFile.json?fileUuid="+_this+"'>预览</a></td></tr>";
									}
									if (str != null) {
										$("#createPickupDetail1 .pickupForm table").append(str);
										i++;
										str = null;
									}
									m = "";
									j++;
								}
							};
							
							tools.addCheckAllEvent($(".html_documentSign .checks"), $(".html_documentSign .checkAll"));
						});
						
						//提货信息合计
						var supervisionQuantitys = 0;
						$.each($("#createPickupDetail1 .supervisionQuantity"), function() {
							supervisionQuantitys += parseFloat($(this).attr("supervisionQuantity"));
						});
						$("#createPickupDetail1 #supervisionQuantitys").html(tools.formatCurrency(supervisionQuantitys.toFixed(2)));
						
						var pickupQuantitys = 0;
						$.each($("#createPickupDetail1 .pickupQuantity"), function() {
							pickupQuantitys += parseFloat($(this).attr("pickupQuantity"));
						});
						$("#createPickupDetail1 #pickupQuantitys").html(tools.formatCurrency(pickupQuantitys.toFixed(2)));
			
			var bulkstockAmount = 0;
						$.each($("#createPickupDetail1 .bulkstockAmount"), function() {
							bulkstockAmount += parseFloat($(this).attr("bulkstockAmount"));
						});
						$("#createPickupDetail1 #bulkstockAmount").html(tools.formatCurrency(bulkstockAmount));
						
						var interest = 0;
						$.each($("#createPickupDetail1 .interest"), function() {
							interest += parseFloat($(this).attr("interest"));
						});
						$("#createPickupDetail1 #interest").html(tools.formatCurrency(interest));
						
						var others = 0;
						$.each($("#createPickupDetail1 .others"), function() {
							others += parseFloat($(this).attr("others"));
						});
						$("#createPickupDetail1 #others").html(tools.formatCurrency(others));
						
						var pickupAmounts = 0;
						$.each($("#createPickupDetail1 .pickupAmount"), function() {
							pickupAmounts += parseFloat($(this).attr("pickupAmount"));
						});
						$("#createPickupDetail1 #pickupAmounts").html(tools.formatCurrency(pickupAmounts.toFixed(2)));
						
					}
					
					var step = 1;
					if (data.datas[0].finalId == 25) {
						step = 1;
					} else if (data.datas[0].finalId == 5) {
						step = 2;
					} else if (data.datas[0].finalId == 3) {
						step = 3;
					} else if (data.datas[0].finalId == 9) {
						step = 4;
					} else if (data.datas[0].finalId == 10) {
						step = 5;
					} else if (data.datas[0].finalId == 20) {
						step = 6;
					} else if (data.datas[0].finalId == 14) {
						step = 7;
					}
					
					tools.stepBar($("#createPickupDetail1StepBar"), step, [{name: "商务部审核", id: 1}, {name: "运营审核", id: 2}, {name: "风控审核", id: 3}, {name: "资金审核", id: 4}, {name: "资金领导审核", id: 5}, {name: "副总经理审核", id: 6}, {name: "审核完成", id: 7}]);
				
				}else {
					base.alert(2, "获取信息失败");
					return;
				}
			}
		});
	};
//	var initPickUpDetail = function(pickupId) {
//		base.fnAjax({
//			action: "inquireFinancePickupApplicationWarehouse.json",
//			async: false,
//			oData: {
//				pickupId: pickupId
//			}, //传递参数
//			fnSuccess: function(data) {
//				if (data.status == 200) {
//					if(data.datas.length > 0) {
//						var pickUpInfoTmpl = $("#createPickupDetail1 #pickUpInfoTmpl").html();
//						var html = juicer(pickUpInfoTmpl, data);
//						$("#createPickupDetail1 .content1").html(html);
//						
//						if(data.datas[0].filesUuid != undefined) {
//							var filesUuid = data.datas[0].filesUuid;
//							var tableBody = "";
//							var index = 0;
//							$.each(filesUuid.split(","), function() {
//								var _this = this;
//								if($.trim(_this) != "") {
//									tableBody += '<tr><td>'+index+'</td><td>'+ tools.inquireFile(_this) +'</td><td><a target="_blank" class="table_details_link" fileUuid="' + _this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + _this + '" >' + "预览" + '</a></td></tr>';
//								}
//								index = index + 1;
//							});
//							
//							$("#createPickupDetail1 .fileUuidBody").html(tableBody);
//							databind.disableInput("#createPickupDetail1");
//						}
//						
//						//提货信息合计
//						var supervisionQuantitys = 0;
//						$.each($("#createPickupDetail1 .supervisionQuantity"), function() {
//							supervisionQuantitys += parseFloat($(this).attr("supervisionQuantity"));
//						});
//						$("#createPickupDetail1 #supervisionQuantitys").html(supervisionQuantitys);
//						
//						var pickupQuantitys = 0;
//						$.each($("#createPickupDetail1 .pickupQuantity"), function() {
//							pickupQuantitys += parseFloat($(this).attr("pickupQuantity"));
//						});
//						$("#createPickupDetail1 #pickupQuantitys").html(pickupQuantitys);
//						
//						var pickupAmounts = 0;
//						$.each($("#createPickupDetail1 .pickupAmount"), function() {
//							pickupAmounts += parseFloat($(this).attr("pickupAmount"));
//						});
//						$("#createPickupDetail1 #pickupAmounts").html(pickupAmounts);
//						
//					}
//				}else {
//					base.alert(2, "获取信息失败");
//					return;
//				}
//			}
//		});
//	};
	
	
	return {
		init: init
	};
});